/*
test_tiny.c
test for index tiny:
1)idx_init(CREATE)
2)idx_insert <<20
3)idx_search <<20
4)idx_exit()
5)idx_init(LOAD)
6)idx_delete <<5
7)idx_insert <<20
8)idx_search <<20+15
9)idx_exit()
10)idx_init(LOAD)
11)idx_search <<35
12)idx_delete <<20
13)idx_search <<15
14)idx_exit()
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "defines.h"
#include "index.h"

char string_1[][20]={"aa","bb","cc","dd","ee","ff","gg","hh","ii","jj","aa0","aa1","aa2","aa3","aa4","aa5","aa6","aa7","aa8","aa9"};
char string_2[][20]={"aaa","abb","acc","add","aee","aff","agg","ahh","aii","ajj","aaa0","aaa1","aaa2","aaa3","aaa4","aaa5","aaa6","aaa7","aaa8","aaa9"};

int main()
{
	char *g_image=NULL;
	INIT_TYPE init_type;
	STATE state;
	int i, disk_location, slot_cnt, last_slot_info;

// part.1 
	sync_init("/tmp/disk","sync_1.log");
	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	state=idx_init(INIT_TYPE_CREATE, g_image, "index_1.log", 5*1024*64);
	printf ("part_1:index_init_done\n\n");
	fflush(stdout);

// part.2 
	for (i=0; i<20; i++) idx_insert(string_1[i],strlen(string_1[i]), i,i,i);
	printf ("part_2:insert 20 done.\n\n");
	fflush(stdout);
	
// part.3
	for (i=0; i<20; i++) 	
	{
		idx_search(string_1[i], strlen(string_1[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	printf ("part_3:idx search 20 done.\n\n");
	fflush(stdout);
	
		
// part.4
	idx_exit();	
	sync_exit();
	mp_free(g_image);
	printf ("part_4:exit done.\n\n");
	fflush(stdout);


// part.5   idx_init(LOAD)
	sync_init("/tmp/disk", "sync_2.log");
	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	state=idx_init(INIT_TYPE_LOAD, g_image, "index_2.log", 5*1024*64);
	printf ("part_5: index_init_done\n\n");
	fflush(stdout);


// part.6   idx_delete
	for (i=15; i<20; i++)
	{
		idx_delete(string_1[i], strlen(string_1[i]), &disk_location);
		printf ("%d\n", disk_location);
	}
	printf ("part_6: idx delete <<5 done\n\n");
	fflush(stdout);

// part.7   idx_insert
	for (i=0; i<20; i++)
	{
		idx_insert(string_2[i], strlen(string_2[i]), i*100,i*100, i*100);
	}
	printf ("part_7: insert [2]<<20 done\n\n");
	fflush(stdout);
	

// part.8   idx_search
	for (i=0; i<15; i++)
	{
		idx_search(string_1[i], strlen(string_1[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	for (i=0; i<20; i++)
	{
		idx_search(string_2[i], strlen(string_2[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	printf ("part.8:idx search 35 done......\n\n");
	fflush(stdout);

	

// part.9  idx_exit
	idx_exit();
	sync_exit();
	mp_free(g_image);
	printf ("part.9:exit....\n\n");
	fflush(stdout);

// part.10 idx_init(LOAD)
	sync_init("/tmp/disk", "sync_3.log");
	g_image=(char*)mp_alloc(IMAGE_K_SIZE*1024);
	state=idx_init(INIT_TYPE_LOAD, g_image, "index_3.log", 5*1024*64);
	printf ("part_10: index_init_done\n\n");
	fflush(stdout);

// part.11 idx_search 35
	for (i=0; i<15; i++)
	{
		idx_search(string_1[i], strlen(string_1[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	for (i=0; i<20; i++)
	{
		idx_search(string_2[i], strlen(string_2[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	printf ("part.11:idx search 35 done......\n\n");
	fflush(stdout);

// part.12 idx_delete << 20
	for (i=0; i<15; i++)
	{
		idx_delete(string_1[i], strlen(string_1[i]), &disk_location);
		printf ("%d\n", disk_location);
	}
	for (i=0; i<5; i++)
	{
		idx_delete(string_2[i], strlen(string_2[i]), &disk_location);
		printf ("%d\n", disk_location);
	}
	printf ("part_12: idx delete <<20 done\n\n");
	fflush(stdout);

// part.13 idx_search << 15
	for (i=5; i<20; i++)
	{
		idx_search(string_2[i], strlen(string_2[i]), &disk_location, &slot_cnt, &last_slot_info);
		printf ("%d\t%d\t%d\n", disk_location, slot_cnt, last_slot_info);
	}
	printf ("part_13: idx search <<15 done\n\n");
	fflush(stdout);

// part.14 idx_exit
	idx_exit();
	sync_exit();
	mp_free(g_image);
	printf ("part.14: exit....\n\n");
	fflush(stdout);

	return 0;
}

